<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/10/25
 * Time: 10:26
 */

namespace app\adminapiv3\service\asset_manage\ministry;


use app\adminapiv3\service\BaseService;
use app\common\service\asset_manage\ministry\HostMinistry;

class MinistryService extends BaseService
{
    public function list()
    {
        $server = new HostMinistry();
        $list = $server->list();

        return $list;
    }

    /**
     * @name 添加
     * @param $name
     * @param $pid
     * @return bool
     * @author wx
     * @date 2019/10/25 13:11
     */
    public function add($name, $pid)
    {
        global $logArray;
        $logArray['content'] = '添加部门';

        //1. 检查重复
        if ($this->checkExist(['name' => $name, 'pid' => $pid])) {
            $this->setError($this->CODE_FAIL,'请勿在同级下面添加相同部门名称');
            return false;
        }

        //2. 查询pid的等级
        $addData = array(
            'pid' => $pid,
            'name' => $name,
            'ctime' => time()
        );

        $server = new HostMinistry();
        $ministryId = $server->add($addData);

        $logArray['new_info'] = '部门名称：'.$name;

        $returnData['name'] = $name;
        $returnData['id'] = $ministryId;

        return $returnData;
    }

    /**
     * @name 检查重复
     * @param $condition
     * @return bool
     * @author wx
     * @date 2019/10/25 13:12
     */
    public function checkExist($condition)
    {
        $server = new HostMinistry();
        $info = $server->findData($condition);

        if(!$info){
            return false;
        }

        return true;
    }

    /**
     * @name 修改
     * @param $id
     * @param $name
     * @return bool
     * @author wx
     * @date 2019/10/25 13:26
     */
    public function modify($id, $name)
    {
        global $logArray;
        $logArray['content'] = '修改部门';

        //1. 顶级部门不能修改
        if ($this->checkExist(['id' => $id, 'pid' => 0])) {
            $this->setError($this->CODE_FAIL, '顶级部门不能修改');
            return false;
        }

        $server = new HostMinistry();
        $info = $server->findData(['id' => $id]);

        if(empty($info)){
            $this->setError($this->CODE_FAIL, '该部门不存在');
            return false;
        }

        //2. 修改当前部门

        $server->modify(['id' => $id], ['name' => $name]);

        $logArray['old_info'] = '部门名称：'.$info['name'];
        $logArray['new_info'] = '部门名称：'.$name;
        return true;
    }

    public function del($id)
    {
        global $logArray;
        $logArray['content'] = '删除部门';

        //1. 顶级部门不能删除
        if ($this->checkExist(['id' => $id, 'pid' => 0])) {
            $this->setError($this->CODE_FAIL, '顶级部门不能删除');
            return false;
        }

        //2. 查询该部门下面是否存在子部门
        $hostMinistry = new HostMinistry();
        $info = $hostMinistry->findData(['id' => $id]);

        if(empty($info)){
            $this->setError($this->CODE_FAIL, '该部门不存在');
            return false;
        }

        if (!empty($hostMinistry->findData(['pid' => $id]))) {
            $this->setError($this->CODE_FAIL, $info['name'].'部门存在下级部门，不能删除');
            return false;
        }



        //2. 查询该部门下面是否有员工,有员工则不能删除
        $managerServer = new ManagerService();
        if ($managerServer->checkExist(['ministry_id' => $id])) {
            $this->setError($this->CODE_FAIL, $info['name'].'部门下面有员工，不能删除');
            return false;
        }

        //3. 删除部门
        $logArray['old_info'] = '部门名称：'.$info['name'];
        $hostMinistry->del(['id' => $id]);

        return true;

    }
}